package com.scoy.honeymei.utils;

import android.util.Log;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

public class TimeUtil {

    private static final long SS = 1000;
    private static final long MS = SS * 60;
    private static final long HourS = MS * 60;
    private static final long DayS = HourS * 24;
    private static final long MonthS = DayS * 30;
    private static final long YearS = MonthS * 12;

    /**
     * 获取当前时间
     *
     * @param format "yyyy-MM-dd HH:mm:ss"
     * @return
     */
    public static String getTime(String format) {
        Date date = new Date();// 创建一个时间对象，获取到当前的时间
        SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.CHINA);// 设置时间显示格式
        sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        String CurrentTime = sdf.format(date);
        return CurrentTime;
    }

    /**
     * 获取时间戳
     *
     * @return
     */
    public static long getLongTime() {
        return System.currentTimeMillis();
    }

    /**
     * 时间戳 返回 格式时间
     *
     * @param times  时间戳
     * @param format format "yyyy-MM-dd HH:mm:ss"
     * @return
     */
    public static String getLongToDate(long times, String format) {
        SimpleDateFormat formatDate = new SimpleDateFormat(format,
                Locale.CHINA);
        formatDate.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        return formatDate.format(times);
    }

    public static String getLongToDatePHP(long times, String format) {
        SimpleDateFormat formatDate = new SimpleDateFormat(format,
                Locale.CHINA);
        formatDate.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        return formatDate.format(times * 1000);
    }

    /**
     * 调此方法输入所要转换的时间输入例如（"2014-06-14-16-09-00"）返回时间戳
     *
     * @param time
     * @return
     */
    public static long dataOne(String time, String format) {
        SimpleDateFormat sdr = new SimpleDateFormat(format, Locale.CHINA);
        sdr.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        Date date;
        long ltime = 0;
        try {
            date = sdr.parse(time);
            ltime = date.getTime();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ltime;
    }


    /**
     * 比较两个日期的大小
     *
     * @param str1   the first date
     * @param str2   the second date
     * @param format 样式"yyyy-MM-dd HH:mm:ss"
     * @return true <br/>false
     */
    public static boolean getDateBigger(String str1, String str2, String format) {
        boolean isBigger = false;
        SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.CHINA);
        sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        Date dt1 = null;
        Date dt2 = null;
        try {
            dt1 = sdf.parse(str1);
            dt2 = sdf.parse(str2);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (dt1.getTime() > dt2.getTime()) {
            isBigger = false;
        } else if (dt1.getTime() <= dt2.getTime()) {
            isBigger = true;
        }
        return isBigger;
    }

    /**
     * 发布时间差
     *
     * @param timestamp 时间戳
     * @return
     */
    public static String getSendTime(long timestamp) {
        long timeMillis = System.currentTimeMillis();
        long timeKK = timeMillis - timestamp;
        if (timeKK > YearS) {
            int time = (int) (timeKK / YearS);
            return time + "年前";
        } else if (timeKK > MonthS) {
            int time = (int) (timeKK / MonthS);
            return time + "个月前";
        } else if (timeKK > DayS) {
            int time = (int) (timeKK / DayS);
            return time + "天前";
        } else if (timeKK > HourS) {
            int time = (int) (timeKK / HourS);
            return time + "小时前";
        } else if (timeKK > MS) {
            int time = (int) (timeKK / MS);
            return time + "分钟前";
        } else if (timeKK > SS) {
            int time = (int) (timeKK / SS);
            return time + "秒前";
        } else {
            return "刚刚";
        }
    }


    /**
     * 获取剩余天数
     *
     * @param timestamp 时间戳
     * @return
     */
    public static int getLeftDay(long timestamp) {
        long timeMillis = System.currentTimeMillis();
        long leftMillins = timestamp - timeMillis;
        return (int) (leftMillins / (3600 * 24 * 1000));
    }


    /**
     * 通过日期获取年龄
     *
     * @param date
     * @return
     */
    public static int getAgeByDay(Date date) {
        // 得到当前时间的年、月、日
        if (date != null) {
            Calendar cal = Calendar.getInstance();
            int yearNow = cal.get(Calendar.YEAR);
            int monthNow = cal.get(Calendar.MONTH) + 1;
            int dayNow = cal.get(Calendar.DATE);
            //得到输入时间的年，月，日
            cal.setTime(date);
            int selectYear = cal.get(Calendar.YEAR);
            int selectMonth = cal.get(Calendar.MONTH) + 1;
            int selectDay = cal.get(Calendar.DATE);
            // 用当前年月日减去生日年月日
            int yearMinus = yearNow - selectYear;
            int monthMinus = monthNow - selectMonth;
            int dayMinus = dayNow - selectDay;
            int age = yearMinus;// 先大致赋值
            if (yearMinus <= 0) {
                age = 0;
            }
            if (monthMinus < 0) {
                age = age - 1;
            } else if (monthMinus == 0) {
                if (dayMinus < 0) {
                    age = age - 1;
                }
            }
            return age;
        }
        return 0;
    }


    /**
     * 日期转Date
     *
     * @param serverTime
     * @param format
     * @return
     */
    public static Date parseServerTime(String serverTime, String format) {
        if (format == null || format.isEmpty()) {
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.CHINESE);
        sdf.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        Date date = null;
        try {
            date = sdf.parse(serverTime);
        } catch (Exception e) {
            Log.e("Exception", e.toString());
        }
        return date;
    }


}
